package com.liangzhong.model.playerCenter
{
	import com.liangzhong.model.playerCenter.vo.PlayerPlaceVo;
	import com.liangzhong.model.playerCenter.vo.PlayerVo;
	import com.liangzhong.model.playerCenter.vo.TrainingTacticsVo;

	/**
	 *获取默认阵型  
	 * @author Administrator
	 * 
	 */	
	public class GetDefaultBattlearray
	{
		public function GetDefaultBattlearray()
		{
		}
		/**
		 *默认阵型   
		 * @param $vo 位置
		 * @param $arr球员数据
		 * 
		 */
		public static function defaultBattlearray($vo:TrainingTacticsVo,$arr:Array):Array
		{
			var arr:Array = $arr.concat();//球员列表 浅复制
			var gk:Array = [];//守门员
			var df:Array = [];//后卫
			var mf:Array = [];//中场
			var fw:Array = [];//前锋
			
			//不同位置的球员
			var gkPlayerVo:Array = playerFilter(arr,1);//守门员
			var dfPlayerVo:Array = playerFilter(arr,2);//后卫
			var mfPlayerVo:Array = playerFilter(arr,3);//中场
			var fwPlayerVo:Array = playerFilter(arr,4);//前锋
			gkPlayerVo.sort(sort);
			dfPlayerVo.sort(sort);
			mfPlayerVo.sort(sort);
			fwPlayerVo.sort(sort);
			function sort(a:PlayerVo,b:PlayerVo):Number
			{
				if(a.ability < b.ability)return 1;
				if(a.ability > b.ability)return -1;
				return 0;
			}
			gk = getsplicePlayer(gkPlayerVo,$vo.GK.length)//,gkPlayerVo);
			df = getsplicePlayer(dfPlayerVo,$vo.DF.length)//,dfPlayerVo);
			mf = getsplicePlayer(mfPlayerVo,$vo.MF.length)//,mfPlayerVo);
			fw = getsplicePlayer(fwPlayerVo,$vo.FW.length)//,fwPlayerVo);
			
			if(fw.length < $vo.FW.length)
			{
				fw = fw.concat(autoComplePlayer(mfPlayerVo,dfPlayerVo,gkPlayerVo,$vo.FW.length - fw.length));
			}
			
			if(mf.length < $vo.MF.length)
			{
				mf = mf.concat(autoComplePlayer(fwPlayerVo,dfPlayerVo,gkPlayerVo,$vo.MF.length - mf.length));
			}
			if(df.length < $vo.DF.length)
			{
				df = df.concat(autoComplePlayer(fwPlayerVo,mfPlayerVo,gkPlayerVo,$vo.DF.length - df.length));
			}
			if(gk.length < $vo.GK.length)
			{
				gk = gk.concat(autoComplePlayer(dfPlayerVo,mfPlayerVo,fwPlayerVo,$vo.GK.length - gk.length));
			}
			var comArr:Array = [];
			comArr = comArr.concat(combination(fw,$vo.FW));
			comArr =comArr.concat(combination(mf,$vo.MF));
			comArr =comArr.concat(combination(df,$vo.DF));
			comArr =comArr.concat(combination(gk,$vo.GK));
			return comArr;		
		}
		/**
		 *组合数据 
		 * @param $playerArr
		 * @param $place
		 * 
		 */		
		private static function combination($playerArr:Array,$place:Array):Array
		{
			var arr:Array = [];
			var len:int = 0;
			var i:int = 0;
			len = $place.length;
			var playerVo:PlayerVo;
			var placeObj:Object;
			var playerPlaceVo:PlayerPlaceVo;
			for( i = 0;i < len;i++)
			{
				playerVo = $playerArr[i];
				if(playerVo == null)continue;
				placeObj = $place[i];
				playerPlaceVo = new PlayerPlaceVo();
				playerPlaceVo.playerid = playerVo.playerid;
				playerPlaceVo.flasx = placeObj.fx;
				playerPlaceVo.flasy = placeObj.fy;
				arr.push(playerPlaceVo);
			}
			return arr;
		}
		/**
		 * 球员分类
		 * @param $arr
		 * @param $type
		 * @return 
		 * 
		 */
		private static function playerFilter($arr:Array,$type:int):Array
		{
			var arr:Array = $arr.filter(filter);
			function filter(element:PlayerVo, index:int, arr:Array):Boolean 
			{
				return (element.place == $type);
			}
			return arr;
		}
		
		/**
		 * 截取数据
		 * @param $playerVo
		 * @param $len
		 * @return 
		 * 
		 */
		private static function getsplicePlayer($playerVo:Array,$len:int):Array
		{
			if($len< $playerVo.length)
				return $playerVo.splice(0,$len);
			return $playerVo.splice(0,$playerVo.length);
		}
		/**
		 *自动补全球员 
		 * 
		 */
		private static function autoComplePlayer($arr_1:Array,$arr_2:Array,$arr_3:Array,$len:int):Array
		{
			var arr:Array = [];
			if($arr_1.length > 0)
			{
				arr = arr.concat(getsplicePlayer($arr_1,$len - arr.length));
			}
			if(arr.length >= $len)return arr;
			if($arr_2.length > 0)
			{
				arr = arr.concat(getsplicePlayer($arr_2,$len - arr.length));
				
			}
			if(arr.length >= $len)return arr;
			if($arr_3.length > 0)
			{
				arr = arr.concat(getsplicePlayer($arr_3,$len - arr.length));
				
			}
			return arr;
		}
	}
}